﻿using NightskyMimic.Domain;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NightskyMimic.Core
{
    public class Human : Strategy
    {
        public List<Attack> GenerateAttackActions(Player player, Player opponent)
        {
            Console.WriteLine(" ");
            List<Attack> result = new List<Attack>();
            List<Creature> canAttack = player.Zone.Creatures.Where(x => !x.Tapped).ToList();
            if (canAttack.Count > 0)
            {
                Console.WriteLine("- - - - - OPPONENT CREATURESS - - - - -");
                showAvailableCreatures(opponent.Zone.Creatures.Where(x => !x.Tapped).ToList());

                Console.WriteLine("- - - - - CHOOSE ATTACKERS - - - - -");
                showAvailableCreatures(canAttack);

                String ans = Console.ReadLine();
                if (ans.Length > 0)
                {
                    List<int> answer = ans.Split(',').Select(x => int.Parse(x)).ToList();
                    foreach (int i in answer)
                    {
                        result.Add(new Attack(canAttack[i]));
                    }
                }
                Console.WriteLine(" ");
            }
            return result;
        }

        public List<Block> GenerateBlockActions(Player player, Player opponent, List<Attack> attack)
        {
            List<Block> result = new List<Block>();
            List<Creature> canBlock = player.Zone.Creatures.Where(x => !x.Tapped).ToList();

            if (canBlock.Count > 0 && attack.Count > 0)
            {
                Console.WriteLine(" ");

                Console.WriteLine("- - - - - POSSIBLE BLOCKERS - - - - -");
                showAvailableCreatures(canBlock);

                foreach (Attack a in attack)
                {
                    Console.WriteLine("CHOOSE BLOCKERS FOR " + a.Target.ToString());
                    String ans = Console.ReadLine();
                    if (ans.Length > 0)
                    {
                        List<int> answer = ans.Split(',').Select(x => int.Parse(x)).ToList();

                        foreach (int i in answer)
                        {
                            result.Add(new Block(canBlock[i], a.Target));
                        }
                    }

                }
                Console.WriteLine(" ");
            }
            return result;
        }

        public void showAvailableCreatures(List<Creature> creatures) 
        {
            int cont = 0;
            foreach (Creature c in creatures)
            {
                Console.WriteLine(cont + " - " + c.ToString());
                cont++;
            }
        }
    }
}